package com.andyou.modules.content.web;

import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.andyou.common.utils.GridVO;
import com.andyou.common.config.Global;
import com.andyou.common.mapper.JsonMapper;
import com.andyou.common.persistence.Page;
import com.andyou.common.web.BaseController;
import com.andyou.common.utils.StringUtils;
import com.andyou.common.utils.excel.ExportExcel;
import com.andyou.common.utils.DateUtils;
import com.google.common.collect.Lists;
import com.andyou.modules.content.entity.ContentReply;
import com.andyou.modules.content.service.ContentReplyService;

/**
 * 回复管理（content_reply）Controller
 * @author lgx
 * @version 2020-03-09
 */
@Controller
@RequestMapping(value = "${adminPath}/content/contentReply")
public class ContentReplyController extends BaseController {

	@Autowired
	private ContentReplyService contentReplyService;
	
	@ModelAttribute
	public ContentReply get(@RequestParam(required=false) String id) {
		ContentReply entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = contentReplyService.get(id);
		}
		if (entity == null){
			entity = new ContentReply();
		}
		return entity;
	}
	
	@RequiresPermissions("content:contentReply:view")
	@RequestMapping(value = {"contentReplyList", ""})
	public String list(ContentReply contentReply, HttpServletRequest request, HttpServletResponse response, Model model) {
		//Page<ContentReply> page = contentReplyService.findPage(new Page<ContentReply>(request, response), contentReply); 
		//model.addAttribute("page", page);
		return "modules/content/contentReplyList";
	}
	
	@RequiresPermissions("content:contentReply:view")
	@ResponseBody
	@RequestMapping(value = { "contentReplyListJson" })
	public String contentReplyListJson(ContentReply contentReply, HttpServletRequest request,
			HttpServletResponse response, Model model) {
	
		Page<ContentReply> page = contentReplyService.findPage(new Page<ContentReply>(request, response,true),
				contentReply);
		model.addAttribute("page", page);

		GridVO gridVO = new GridVO();
		gridVO.setRows(page.getList());
		gridVO.setTotal(page.getCount());
		
		return JsonMapper.getInstance().toJson(gridVO);
	}

	@RequiresPermissions("content:contentReply:view")
	@RequestMapping(value = "contentReplyForm")
	public String form(ContentReply contentReply, Model model) {
		model.addAttribute("contentReply", contentReply);
		return "modules/content/contentReplyForm";
	}
	
	@RequiresPermissions("content:contentReply:view")
	@RequestMapping(value = "contentReplyDetail")
	public String contentReplyDetail(ContentReply contentReply, Model model){
		model.addAttribute("contentReply", contentReply);
		return "modules/content/contentReplyDetail";
	}

	@RequiresPermissions(value={"content:contentReply:edit","content:contentReply:create"},logical = Logical.OR)
	@RequestMapping(value = "save")
	public String save(ContentReply contentReply, Model model, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, contentReply)){
			return form(contentReply, model);
		}
		contentReplyService.save(contentReply);
		//renderStringByJbox(response, "提交成功");
		//return null;
		return "redirect:"+Global.getAdminPath()+"/content/contentReply/?repage";
		
	}
	
	@RequiresPermissions("content:contentReply:delete")
	@RequestMapping(value = "delete")
	public String delete(ContentReply contentReply, RedirectAttributes redirectAttributes) {
		contentReplyService.delete(contentReply);
		addMessage(redirectAttributes, "删除回复管理成功");
		return "redirect:"+Global.getAdminPath()+"/content/contentReply/?repage";
	}
	
	@RequiresPermissions("content:contentReply:delete")
    @ResponseBody
	@RequestMapping(value = "deleteAjax")
	public String deleteAjax(ContentReply contentReply, RedirectAttributes redirectAttributes) {
		contentReplyService.delete(contentReply);
		return "删除成功";
	}
	
	@RequiresPermissions("content:contentReply:delete")
	@ResponseBody
	@RequestMapping(value = "batchDelete") 
	public String batchDelete(String ids, RedirectAttributes redirectAttributes) {
		
        String[]  idsArr =  ids.split(",");
        List<String> idList = Lists.newArrayList();
        idList = Arrays.asList(idsArr);
        
        contentReplyService.batchDelete(idList);
	
		return "删除成功";
	}
	
	@RequestMapping(value = "export")
	public String exportFile(ContentReply contentReply, HttpServletRequest request,
			HttpServletResponse response, RedirectAttributes redirectAttributes) {
		try {
			String fileName = "回复管理" + DateUtils.getDate("yyyyMMddHHmmss") + ".xlsx";
			Page<ContentReply> page_ = new Page<ContentReply>(request, response, -1);
			page_.setPageSize(500000);
			contentReply.setPage(page_);
			page_ = contentReplyService.findPage(page_, contentReply);
			new ExportExcel("回复管理", ContentReply.class).setDataList(page_.getList())
					.write(response, fileName).dispose();
			return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "导出数据失败！失败信息：" + e.getMessage());
		}
		return "redirect:" + Global.getAdminPath() + "/content/contentReply/?repage";
	}

}